<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
require_once('includes/configure.php');

//Establish connection to mysql server.
$conector = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die(mysql_error());
mysql_select_db(DB_DATABASE) or die(mysql_error());

//Inhale the database tablenames.
require_once('includes/database_tables.php');

//Inhale the configuration records from database.
$sqlQuery = 'select configuration_key as cfgKey, configuration_value as cfgValue from ' . TABLE_CONFIGURATION;
$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
while($row = mysql_fetch_array($dataReturned))
{
	define($row['cfgKey'], $row['cfgValue']);
}

function actionComponent(&$params, $action='query',$validate = true)
{
	define('IN_COMPONENT_CONTROLLER', 'true');
	define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

	//Validate Account
	require_once('includes/components/AccountComponent.php');
	$component = new AccountComponent();
	$params['target_accountId'] = 0;
	$validateArray = $component->validate($params);
	$accountId = $validateArray['accountId'];
	$account_session = $validateArray['accountSession'];
	$isAdministrator = $validateArray['isAdministrator'];
	
	$params['accountId'] = $accountId;
	$params['accountSession'] = $account_session;
	$params['isAdministrator'] = $isAdministrator;

	if ($accountId < 0 || (returnBoolean(REQUIRE_ADMINISTRATOR) && !$isAdministrator))
	{
		$params['handle'] = 'AccountComponent';
		$action='query';
	}

	//Set the current language and inhale the language definitions
	$language_code = DEFAULT_LANGUAGE;
	$sqlQuery = 'select languages_id from ' . TABLE_LANGUAGES . ' where code=\'' . $language_code . '\'';
	$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
	while($row = mysql_fetch_array($dataReturned))
	{
		$language_id = (int)$row['languages_id'];
		break;
	}
	require_once('includes/languages/'.$language_code.'.php');
	$params['languageId'] = $language_id;

	//Set the current currency
	$currency_code = DEFAULT_CURRENCY;
	$sqlQuery = 'select currencies_id from ' . TABLE_CURRENCIES . ' where code=\'' . $currency_code . '\'';
	$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
	while($row = mysql_fetch_array($dataReturned))
	{
		$currency_id = (int)$row['currencies_id'];
		break;
	}
	$params['currencyId'] = $currency_id;

	$handle = prepare_input($params['handle']);

	if ($handle == 'MultipleResultsComponent')
	{
		$handles = $params['handles'];
		if ($handles != null)
		{
			$handleArray = explode(',', $handles);
			foreach ($handleArray as $handleValue)
			{
				$handleValue = trim($handleValue);
    				$params['handle'] = $handleValue;

				include_once('includes/components/' . $handleValue . '.php');
				$component = new $handleValue;
				$return = $component->action($params, $action);
				if ($value == null) {
					$value = $return;
				}
				else if ($return != null) {
					$value = array_merge($value, $return);
				}
			}
		}
	}
	else
	{
		include_once('includes/components/' . $handle . '.php');
		$component = new $handle;
		$return = $component->action($params, $action);
		if ($value == null) {
			$value = $return;
		}
		else if ($return != null) {
			$value = array_merge($value, $return);
		}
	}

	return $value;
}
?>
